package www.sofn.com.shopping.cart.handler;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import www.sofn.com.shopping.cart.domain.pojo.ShoppingCartPO;

import java.io.IOException;
import java.util.List;

@Slf4j
@MappedTypes({Object.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class AttrInfoFieldTypeHandler extends AbstractJsonTypeHandler<Object> {
    private static ObjectMapper objectMapper=new ObjectMapper();
    public AttrInfoFieldTypeHandler(Class<?> type) {
        if (log.isTraceEnabled()) {
            log.trace("SpecsInfoFieldTypeHandler(" + type + ")");
        }
        Assert.notNull(type, "Type argument cannot be null");
    }

    @Override
    protected Object parse(String json) {
       try {
        return objectMapper.readValue(json, new TypeReference<List<ShoppingCartPO.AttrInfo>>() {
        });
       }catch (IOException var3){
           throw new RuntimeException(var3);
       }


    }

    @Override
    protected String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        }catch (JsonProcessingException var3){
            throw new RuntimeException(var3);
        }
    }
}
